<?php

namespace app\admin\model;

use app\common\model\Area;
use think\Model;


class Cusaddress extends Model
{


    // 表名
    protected $name = 'cusaddress';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'genderstatus_text',
        'tagstatus_text',
        'city_id',
        'province_id',
        'district_id',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getCityAttr($value, $data)
    {
        if ($data['city']) {
            return self::gettitle($data['city']);
        } else {
            return '-';
        }
    }

    public function getProvinceAttr($value, $data)
    {
        if ($data['province']) {
            return self::gettitle($data['province']);
        } else {
            return '-';
        }
    }

    public function getDistrictAttr($value, $data)
    {
        if ($data['district']) {
            return self::gettitle($data['district']);
        } else {
            return '-';
        }
    }


    public function gettitle($id)
    {
        return Area::get($id)['name'];
    }


    public function getGenderstatusList()
    {
        return ['1' => __('Genderstatus 1'), '2' => __('Genderstatus 2')];
    }

    public function getTagstatusList()
    {
        return ['1' => __('Tagstatus 1'), '2' => __('Tagstatus 2'), '3' => __('Tagstatus 3')];
    }


    public function getGenderstatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['genderstatus']) ? $data['genderstatus'] : '');
        $list = $this->getGenderstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTagstatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tagstatus']) ? $data['tagstatus'] : '');
        $list = $this->getTagstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDistrictIdAttr($value, $data)
    {
        return $data['district'];
    }

    public function getProvinceIdAttr($value, $data)
    {
        return $data['province'];
    }

    public function getCityIdAttr($value, $data)
    {
        return $data['city'];
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
